<?php

use think\migration\Seeder;
use app\common\model\Category;

class InitDefaultCategory extends Seeder
{
    /**
     * Run Method.
     *
     * Write your database seeder using this method.
     *
     * More information on writing seeders is available here:
     * http://docs.phinx.org/en/latest/seeding.html
     */
    public function run()
    {
        $lists = [
            [
                'name' => 'NEWS CENTER',
                'title' => '新闻中心',
                'slug' => 'news',
                'children' => [
                    [
                        'title' => '综合资讯',
                    ], [
                        'title' => '行业动态',
                    ]
                ],
            ], [
                'name' => 'NOTICE',
                'title' => '通知通告',
                'slug' => 'notice',
                'children' => [
                    [
                        'title' => '通知通告',
                    ], [
                        'title' => '政策法规',
                    ], [
                        'title' => '文件下载',
                    ],
                ],
            ], [
                'name' => 'SPECIAL COLUMN',
                'title' => '专题专栏',
                'slug' => 'column',
                'children' => [
                    [
                        'title' => '国际专栏',
                    ], [
                        'title' => '技能大赛',
                    ], [
                        'title' => '师资培训',
                        'is_subscribe' => 1,
                        'is_attached' => 1,
                    ], [
                        'title' => '项目申报',
                        'is_applied' => 1,
                        'is_attached' => 1,
                    ],
                ],
            ], [
                'name' => 'ACADEMY OF SCIENTIFIC RESEARCH',
                'title' => '科学研究',
                'slug' => 'research',
                'children' => [
                    [
                        'title' => '专题研究',
                    ], [
                        'title' => '研究与出版',
                    ]
                ],
            ], [
                'name' => 'SOCIAL SERVICE',
                'title' => '社会服务',
                'slug' => 'service',
                'children' => [
                    [
                        'title' => '项目合作',
                    ]
                ],
            ]
        ];
        $this->sendCategoryList($lists, 0);
    }

    public function sendCategoryList($lists, $parent_id){
        $CategoryModel = new Category;
        $default_data = ['status' => 1, 'is_applied' => 0, 'is_subscribe' => 0, 'is_attached' => 0, 'member_id' => 1, 'parent_id' => $parent_id];
        foreach($lists as $key => $item){
            $res = $CategoryModel->findOrCreate(array_merge($default_data, $item));
            if(!$res['status']){
                // 添加或更新失败
                continue;
            }
            if(isset($item['children'])){
                $res_data = $res['data'];
                if(!isset($res_data['id']) || $res_data['id'] < 1){
                    continue;
                }
                $this->sendCategoryList($item['children'], $res_data['id']);
            }
        }
    }
}